<template>
  <div>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="sid"
        label="学号"
        width="150">
      </el-table-column>
      <el-table-column
        prop="sname"
        label="姓名"
        width="120">
      </el-table-column>
      <el-table-column
        prop="password"
        label="密码"
        width="120">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-popconfirm
            confirm-button-text='删除'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="删除不可复原"
            @confirm="deleteStudent(scope.row)"
          >
            <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
          <el-button @click="editor(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
</template>

<script>

import axios from 'axios'

export default {
  name: 'studentList',
  data () {
    return {
      tableData: null,
      pageSize: 7,
      total: null,
      ruleForm: null,
      tmpList: null
    }
  },
  created () {
    if (this.tmpList !== null) {
      this.tmpList = null
    }

    // 是否从查询页面携带数据跳转过来
    this.ruleForm = this.$route.query.ruleForm

    if (this.$route.query.ruleForm === undefined || (this.ruleForm.sid === null && this.ruleForm.sname === null)) {
      axios.get('http://localhost:9000/student/getLength').then(res => {
        this.total = res.data
      })

      axios.get('http://localhost:9000/student/findByPage/0/' + this.pageSize).then(res => {
        this.tableData = res.data
      })
    } else {
      // 从查询页跳转并且含查询
      console.log('正在查询跳转数据')
      console.log(this.ruleForm)
      axios.post('http://localhost:9000/student/findBySearch', this.ruleForm).then(res => {
        this.tmpList = res.data
        this.total = res.data.length
        console.log(this.tmpList)
        let start = 0, end = this.pageSize
        let length = this.tmpList.length
        let ans = end < length ? end : length
        this.tableData = this.tmpList.slice(start, ans)
      })
    }

  },
  methods: {
    editor (row) {
      this.$router.push({
        path: 'editorStudent',
        query: {
          sid: row.sid,
        }
      })
    },
    deleteStudent (row) {
      axios.delete('http://localhost:9000/student/deleteById/' + row.sid).then(res => {
        if (res.data) {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          if (this.tmpList === null) {
            window.location.reload()
          } else {
            this.$router.push('/queryStudent')
          }
        } else {
          this.$message({
            showClose: true,
            message: '删除出错，请查询数据库连接',
            type: 'error'
          })
        }
      }).catch(e => {
        this.$message({
          showClose: true,
          message: '删除出错，存在外键依赖' + e,
          type: 'error'
        })
      })
    },
    changePage (page) {
      page = page - 1

      if (this.tmpList === null) {
        axios.get('http://localhost:9000/student/findByPage/' + page + '/' + this.pageSize).then(res => {
          this.tableData = res.data
        })
      } else {
        let that = this
        let start = page * that.pageSize, end = that.pageSize * (page + 1)
        let length = that.tmpList.length
        let ans = end < length ? end : length
        that.tableData = that.tmpList.slice(start, ans)
      }
    },

  }
}
</script>

<style scoped>

</style>
